package net.javalib.flickr.search.adapters;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.javalib.flickr.search.DownloadInfo;
import net.javalib.flickr.search.R;
import net.javalib.flickr.search.activities.MainActivity;
import net.javalib.flickr.search.flickr.FlickrImage;
import net.javalib.flickr.search.util.ViewWrapperHelper;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * ImageGrid Adapter
 */
public class ImagesGridAdapter extends BaseAdapter {
	/** The activity */
	private MainActivity activity;
	/** Cache of images */
	private List<FlickrImage> images = new ArrayList<FlickrImage>();
	/** Cache of views */
	private Map<Long, View> views = new HashMap<Long, View>();

	/**
	 * Constructor
	 */
	public ImagesGridAdapter(MainActivity activity) {
		this.activity = activity;
	}
	
	/**
	 * Sets the images
	 */
	public void setImages(List<FlickrImage> images, ViewGroup parent) {
		this.images.clear();
		this.views.clear();
		this.images.addAll(images);
		for (int x=0;x<images.size();x++) {
			// pre-cache views
			getView(x, null, parent);
		}
		notifyDataSetChanged();
	}

	public void refresh() {
		for (View view : views.values()) {
			MyViewWrapper wrapper = (MyViewWrapper) view.getTag();
			wrapper.refresh();
		}
	}
	
	/**
	 * Returns the image at the given position
	 */
	public FlickrImage getImage(int position) { return images.get(position); }

	/**
	 * Returns the view for the given image
	 */
	public View getView(FlickrImage image) { return views.get(image.id); }
	
	/**
	 * Clears adapter
	 */
	public void clear() {
		images.clear();
		views.clear();
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() { return images.size(); }
	@Override
	public Object getItem(int position) { return getImage(position); }
	@Override
	public long getItemId(int position) { return getImage(position).id; }
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final FlickrImage image = getImage(position);
		View view = views.get(image.id);
		if (view != null)
			return view;
		ViewWrapperHelper wrapper = null;
		if (view == null) {
			view = activity.getLayoutInflater().inflate(R.layout.thumbnail_card, parent, false);
			wrapper = new MyViewWrapper(view,image); 
			view.setTag(wrapper);
			views.put(image.id, view);
		} else {
			wrapper = (ViewWrapperHelper) view.getTag();
		}
		
		wrapper.refresh();
		
		return view;
	}
	
	class MyViewWrapper extends ViewWrapperHelper {
		
		public MyViewWrapper(View view, FlickrImage image) {
			super(view);
			tag = image;
		}
		
		@Override
		public void refresh() {
			FlickrImage image = (FlickrImage) tag;
			ImageView imageView = (ImageView) getView(R.id.thumbnail_image);
			imageView.setImageBitmap(activity.app.getImageBitmap(DownloadInfo.getCacheKey(image, true)));
			
			TextView title = (TextView) getView(R.id.thumbnail_title);
			title.setText(image.title);
			
			ProgressBar progress = (ProgressBar) getView(R.id.thumbnail_progress);
			progress.setMax(100);
			if (image.error) {
				progress.setBackgroundColor(Color.RED);
				progress.setVisibility(View.VISIBLE);
			} else if (image.downloaded) {
				progress.setVisibility(View.GONE);
			} else {
				progress.setVisibility(View.VISIBLE);
			}
		}
		
	}
	
}
